package frontend.visitor.symboltbl.scope;

import exceptions.VisitInnerException;
import frontend.visitor.symboltbl.ScopeStackTrace;
import frontend.visitor.symboltbl.Symbol;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public abstract class Scope {
    protected final Map<String, Symbol> symbolMap = new HashMap<>();
    protected final ArrayList<Scope> children = new ArrayList<>();
    protected ScopeStackTrace scopeStackTrace;

    public Scope getParentScope() {
        return scopeStackTrace.getParentScope();
    }
    public ArrayList<Scope> getChildrenScope() {
        return children;
    }
    public int getChildrenNum() {
        return children.size();
    }
    public void addChildScope(Scope scope) {
        children.add(scope);
    }
    public ScopeStackTrace getScopeStackTrace() {
        return scopeStackTrace;
    }
    public void setScopeStackTrace(ScopeStackTrace scopeStackTrace) {
        this.scopeStackTrace = scopeStackTrace;
    }
    public abstract String getIndividualName();
    public String getName() {
        return scopeStackTrace.getTrace();
    }

    public boolean duplicateCheck(String ident) {
        return !symbolMap.containsKey(ident);
    }
    public void putSymbol(String ident, Symbol symbol) {
        if (!duplicateCheck(ident)) {
            throw new VisitInnerException();
        }
        symbolMap.put(ident, symbol);
    }
    public Symbol findSymbol(String ident) {
        return symbolMap.get(ident);
    }

}
